<template>
	<div class="system-menu-container">
		<div class="breadcrumb-box">
			<Breadcrumb />
		</div>

		<div class="header-search flex space-between">
			<div>
				<el-input class="search-item" placeholder="请输入用户名称" v-model="state.apiData.search" size="default"
					style="width: 220px;" clearable>
				</el-input>
				<el-select v-model="state.apiData.departmentId" placeholder="请选择科室" size="default" clearable filterable style="width:200px;margin-right: 20px;">
					<el-option v-for="(item, index) in state.departmentList" :key="index" :label="item.departmentName"
						:value="item.id"></el-option>
				</el-select>

				<el-button size="default" type="primary" @click="searchChange()" >查询</el-button>
			</div>

			<el-button size="default" type="primary" @click="addUser()">新增用户</el-button>
		</div>

		<div class="page-main" v-loading="state.tableData.loading" ref="pageMain">
			<el-card shadow="hover" style="height:100%;">

				<el-table :data="state.tableData.data" border style="width: 100%" :height="state.tableHeight"
					ref="multipleTableRef">
					<el-table-column label="序号" type="index" width="100" show-overflow-tooltip align="center"></el-table-column>
					<el-table-column prop="name" label="姓名" show-overflow-tooltip align="center"></el-table-column>
					<el-table-column prop="phone" label="电话" width="120" show-overflow-tooltip align="center">
						<template #default="scope">
							{{ scope.row.phone ? scope.row.phone : '-' }}
						</template>
					</el-table-column>
					<el-table-column label="email" show-overflow-tooltip align="center">
						<template #default="scope">
							{{ scope.row.email ? scope.row.email : '-' }}
						</template>
					</el-table-column>
					<el-table-column label="状态" align="center">
						<template #default="scope">
							{{ scope.row.status ? '启用' : '停用' }}
						</template>
					</el-table-column>
					<el-table-column label="操作" width="180" fixed="right" align="center">
						<template #default="scope">
							<el-button size="small" link type="primary" @click="editUser(scope.row)">修改</el-button>
							<el-button v-if="!scope.row.status" size="small" link type="primary"
								@click="changeStatus(scope.row.id, true)">启用</el-button>
							<el-button v-else size="small" link type="primary" @click="changeStatus(scope.row.id, false)"
								style="color:red;">停用</el-button>
							<el-button size="small" link type="primary" @click="resetPwd(scope.row.id)">重置密码</el-button>
						</template>
					</el-table-column>
				</el-table>


				<div class="pagination-box">
					<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
						:pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.apiData.page" background
						v-model:page-size="state.apiData.pageSize" layout="total, sizes, prev, pager, next, jumper"
						:total="state.tableData.total">
					</el-pagination>
				</div>

			</el-card>
		</div>

		<DetailDialog ref="DetailDialogRef" @refresh="getList()" />
	</div>
</template>

<script setup name="system-user">
import { getCurrentInstance } from 'vue';
import { systemApi } from '/@/api/system/index';
import { ElMessageBox, ElMessage } from 'element-plus';
import { toolsApi } from '/@/api/tools'
// 引入组件
const Breadcrumb = defineAsyncComponent(() => import('/@/layout/navBars/breadcrumb/breadcrumb.vue'));
const DetailDialog = defineAsyncComponent(() => import('/@/views/system/user/dialog.vue'));

// 定义变量内容
const DetailDialogRef = ref()
const state = reactive({
	apiData: {
		page: 1,
		pageSize: 10,
		search: '',
		departmentId: ''
	},
	tableData: {
		data: [],
		loading: false,
		total: 0
	},
	tableHeight: '30vh',
});

// 页面加载时
onMounted(() => {
	getDepatment()
	getList()
	state.tableHeight = getCurrentInstance().refs.pageMain.offsetHeight - 130 - 52 + "px";
});

const searchChange = () => {
	state.apiData.page = 1
	getList()
}









// 分页改变
const onHandleSizeChange = (val) => {
	state.apiData.pageSize = val;
	getList();
};
const onHandleCurrentChange = (val) => {
	state.apiData.page = val;
	getList();
};


// 获取科室
const getDepatment = () => {
  toolsApi().departmentList({ all: 1 }).then(res => {
    state.departmentList = res.data
  })
}





// 获取列表
const getList = () => {
	state.tableData.loading = true
	systemApi().systemUserList(state.apiData).then(res => {
		console.log(res.data.items)
		state.tableData.loading = false
		// res.data.items.forEach(item => {
		// 	const departmentNameList = []
		// 	item.department.forEach(it =>{
		// 		departmentNameList.push(it.departmentName + '('+ it.departmentNo + ')')
		// 	})
		// 	item.departmentStr = departmentNameList.length?departmentNameList.join(','):'-'
		// })
		state.tableData.data = res.data.items
		state.tableData.total = res.data.total
	}).catch(() => {
		state.tableData.loading = false
	})

}

// 重置密码
const resetPwd = (id) => {
	ElMessageBox.confirm(`是否重置密码?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			systemApi().resetPassword({ uid: id }).then(res => {
				ElMessage.success('操作成功');
				getList();
			}).catch(() => {

			})

		})
		.catch(() => { });
}

const addUser = () => {
	DetailDialogRef.value.openDialog();
}

const editUser = (row) => {
	DetailDialogRef.value.openDialog(row);
}



// 切换状态
const changeStatus = (id, status) => {
	ElMessageBox.confirm('请确认是否切换状态', '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			systemApi().changeStatus({ uids: [id], status: status }).then(res => {
				ElMessage.success('操作成功');
				getList();
			}).catch(() => {

			})

		})
		.catch(() => { });
}
</script>

<style lang="scss" scoped>
.page-main {
	height: calc(100vh - 50px - 106px);
}
</style>
